package com.ss.isc.schedule.collect.job;

import com.alibaba.fastjson.JSON;
import com.ss.isc.util.StringUtils;
import com.ss.isc.util.constant.HttpConstant;
import com.ss.isc.util.http.BaseHttpUtil;
import com.ss.tools.FileUtils;
import com.ss.utils.DeviceUtil;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileFilter;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件夹变化监听
 * @author 李爽超 chao
 * @create 2019/09/16
 * @email lishuangchao@ss-cas.com
 **/
@Component
public class FileObserver implements ApplicationRunner {

    private static final Logger log = LoggerFactory.getLogger(FileObserver.class);

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception{

        //监听路径
        String filePath= DeviceUtil.getCutFlowUrl() + "/";
        FileFilter filter= FileFilterUtils.and((IOFileFilter) new MyFileFilter());
        FileAlterationObserver filealtertionObserver=new FileAlterationObserver(filePath, filter);
        filealtertionObserver.addListener(new FileAlterationListenerAdaptor(){
            /**
             * 监听图片创建
             * @param file
             */
            @Override
            public void onFileCreate(File file)
            {
                try {
                    //图片base64
                    String base64ByUrl = FileUtils.getBase64ByUrl(DeviceUtil.getCutFlowAgentUrl() + "/" + file.getParentFile().getName() + "/" + file.getName());
                    //设备编号
                    String groupId = file.getParentFile().getName();
                    Map<String, Object> paramJson = new HashMap<String, Object>();
                    paramJson.put("image", base64ByUrl);
                    paramJson.put("image_type", "BASE64");
                    paramJson.put("group_id", groupId);
                    paramJson.put("user_id", "1000");
                    String resultString = BaseHttpUtil.httpPost(JSON.toJSONString(paramJson), "http://47.105.170.76:3000/mock/13" + HttpConstant.FACE_REGISTER, null);
                    if (StringUtils.checkSuccess(JSON.parseObject(resultString))){
                        log.info("人脸注册成功！");
                    }
                } catch (Exception e) {
                    log.error("抽帧图片请求异常", e);
                }
                super.onFileCreate(file);
            }

        });

        FileAlterationMonitor filealterationMonitor=new FileAlterationMonitor(1000);
        filealterationMonitor.addObserver(filealtertionObserver);
        //开始监听
        filealterationMonitor.start();
        log.info("设备抽帧图片监听开启");
    }

}

class MyFileFilter implements IOFileFilter{

    @Override
    public boolean accept(File file)
    {
        return true;
    }

    @Override
    public boolean accept(File dir, String name)
    {
        return true;
    }

}
